package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yimi on 2016/12/19.
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUserById")
    public String getUserById(ModelMap model, Integer id) {

        User user = userService.getUserById(1);
        return outPutString(user);
    }

    @RequestMapping(value = "/addUser")
    public String addUser(ModelMap modelMap){
        userService.addUser("一米长11",22);
        return outPutString("新增用户成功!");
    }


    @RequestMapping(value = "/updateUser")
    public String updateUser(ModelMap modelMap,Integer id){
        if(id == null){
            return outPutString("请输入Id");
        }else {
            userService.updateUser(id);
        }
        return outPutString("更新用户数据成功.");
    }

    @RequestMapping(value = "/deleteUser")
    public String deleteUser(ModelMap modelMap,Integer id){
        if(id == null){
            return outPutString("请输入Id");
        }else {
            userService.deleteUser(id);
        }
        return outPutString("删除成功");
    }


    private String outPutString(Object obj){
        Map<String, Object> map = new HashMap();
        map.put("data",obj);
        return JSON.toJSONString(map);
    }
}
